import json
import requests
from tools import config
from tools.RsaClass import rsaclass


class Account:
    def __init__(self):
        self.headers = config.headers
        self.domain = config.sDomain
        self.product = config.sProduct
        self.time = config.sTime
        self.password = config.password

    def createAccount(self, account_name, job_no, nickname, passchange_req='2', pass_type='0', unit_id='default'):
        '''
        创建账号
        :param account_name: 账号名
        :param job_no: 工号
        :param nickname: 姓名
        :param passchange_req: 密码策略
        :param pass_type: 密码明文、密文
        :param unit_id: 部门ID
        '''
        src = "account_name=" + account_name + "&domain=" + self.domain + "&job_no=" + job_no + "&nickname=" + nickname + "&pass_type=" + pass_type \
              + "&passchange_req=" + passchange_req + "&password=" + self.password + "&product=" + self.product + "&time=" + self.time + "&unit_id=" + unit_id
        # print(src)
        # 数字签名
        sSign = rsaclass(src)
        # print(sSign)
        # 封装参数
        param = {
            "account_name": account_name,
            "domain": self.domain,
            "job_no": job_no,
            "nickname": nickname,
            "pass_type": pass_type,
            "passchange_req": passchange_req,
            "password": self.password,
            "product": self.product,
            "time": self.time,
            "unit_id": unit_id,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/account/createAccount", params=param, headers=self.headers)
        # 使用json.loads将json字符串转成python字典对象
        return json.loads(resp.content.decode())

    def deleteAccountSim(self, account_name):
        '''
        删除账号（保留7天）
        '''
        src = "account_name=" + account_name + "&domain=" + self.domain + "&product=" + self.product + "&time=" + self.time
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "account_name": account_name,
            "domain": self.domain,
            "product": self.product,
            "time": self.time,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/account/deleteAccountSim", params=param,
                             headers=self.headers)
        return json.loads(resp.content.decode())

    def getAccount(self, account_name):
        '''
        获取账号信息
        '''
        src = "account_name=" + account_name + "&domain=" + self.domain + "&product=" + self.product + "&time=" + self.time
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "account_name": account_name,
            "domain": self.domain,
            "product": self.product,
            "time": self.time,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/account/getAccount", params=param,
                             headers=self.headers)
        return json.loads(resp.content.decode())

    def moveUnit(self, account_name, unit_id):
        '''
        转移部门
        '''
        src = "account_name=" + account_name + "&domain=" + self.domain + "&product=" + self.product + "&time=" + self.time + "&unit_id=" + unit_id
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "account_name": account_name,
            "domain": self.domain,
            "product": self.product,
            "time": self.time,
            "unit_id": unit_id,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/account/moveUnit", params=param,
                             headers=self.headers)
        return json.loads(resp.content.decode())

    def suspendAccount(self, account_name):
        '''
        禁用账号
        '''
        src = "account_name=" + account_name + "&domain=" + self.domain + "&product=" + self.product + "&time=" + self.time
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "account_name": account_name,
            "domain": self.domain,
            "product": self.product,
            "time": self.time,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/account/suspendAccount", params=param,
                             headers=self.headers)
        return json.loads(resp.content.decode())

    def recoverAccount(self, account_name):
        '''
        恢复账号
        '''
        src = "account_name=" + account_name + "&domain=" + self.domain + "&product=" + self.product + "&time=" + self.time
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "account_name": account_name,
            "domain": self.domain,
            "product": self.product,
            "time": self.time,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/account/recoverAccount", params=param,
                             headers=self.headers)
        return json.loads(resp.content.decode())

    def updateAccount(self, account_name, addr_right='1', addr_visible='1', exp_time='', job_no='', mobile='', nickname=''):
        '''
        更新账号参数
        :param account_name: 待更新账号名
        :param kwargs --> str:传入值都为str
            addr_right：是否有查看企业通讯录权限，0-没有，1-有
            addr_visible：是否在通讯录中显示：0-不显示，1-显示
            exp_time：过期时间
            job_no：工号
            mobile：手机号
            nickname：姓名(必填，否则默认为空)
        '''
        src = "account_name=" + account_name + "&addr_right=" + addr_right + "&addr_visible=" + addr_visible + "&domain=" \
              + self.domain + "&exp_time=" + exp_time + "&job_no=" + job_no + "&mobile=" + mobile + "&nickname=" + nickname \
              + "&product=" + self.product + "&time=" + self.time
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "account_name": account_name,
            "addr_right": addr_right,
            "addr_visible": addr_visible,
            "domain": self.domain,
            "exp_time": exp_time,
            "job_no": job_no,
            "mobile": mobile,
            "nickname": nickname,
            "product": self.product,
            "time": self.time,
            "sign": sSign,
        }

        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/account/updateAccount", params=param,
                             headers=self.headers)
        return json.loads(resp.content.decode())

    def updatePassword(self, account_name, passchange_req='2'):
        src = "account_name=" + account_name + "&domain=" + self.domain + "&passchange_req=" + passchange_req + \
              "&password=" + self.password + "&product=" + self.product + "&time=" + self.time
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "account_name": account_name,
            "domain": self.domain,
            "passchange_req": passchange_req,
            "password": self.password,
            "product": self.product,
            "time": self.time,
            "sign": sSign,
        }
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/account/updatePassword", params=param,
                             headers=self.headers)
        return json.loads(resp.content.decode())

    def updateRsLimit(self, account_name, rs_limit='-1'):
        '''
        设置邮件收发限制
        :param account_name: 账号
        :param rs_limit: 邮件收发限制类型，17-只能发送域内邮件，收信无限制
                                         18-只能接受域内邮件，发信无限制
                                         19-只能收发域内邮件
                                         -1-无限制
        '''
        src = "account_name=" + account_name +  "&domain=" + self.domain + "&product=" + self.product + "&rs_limit=" + rs_limit +  "&time=" + self.time
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "account_name": account_name,
            "domain": self.domain,
            "rs_limit": rs_limit,
            "product": self.product,
            "time": self.time,
            "sign": sSign,
        }
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/mailaccount/updateRsLimit", params=param,
                             headers=self.headers)
        # print(resp.request.url)
        return json.loads(resp.content.decode())


if __name__ == '__main__':
    ops_obj = Account()
    # 创建账号
    # print(ops_obj.createAccount('mail-ops-test2', '20849-2', '邮箱自动化测试账号2', unit_id='default'))
    # 删除账号
    # ops_obj.deleteAccountSim('mail-ops-test')
    # 查看账号
    info = ops_obj.getAccount('jianbin.zhang')
    print(info)
    # 查看账号部门ID
    # print(info['con']['unit_id'])
    # 转移部门
    # print(ops_obj.moveUnit('mail-ops-test', '1715455'))
    # 禁用账号
    # ops_obj.suspendAccount('mail-ops-test')
    # 恢复账号
    # ops_obj.recoverAccount('mail-ops-test')
    # 更新账号
    # print(ops_obj.updateAccount('mail-ops-test', job_no='ops_20849', nickname='自动化测试'))
    # 重置密码
    # print(ops_obj.updatePassword('mail-ops-test'))
    # 设置账号收发限制
    # print(ops_obj.updateRsLimit('mail-ops-test', '17'))